package tankgame;

import java.util.Vector;

/**
 * @author: lizhiwen
 * @version: 1.0
 */
public class MyTank extends Tank implements Runnable{
    Shot shot = null;
    int liveNum = 2;
    Vector<Shot> shots = new Vector<>();
    Vector<Wall> walls = new Vector<>();

    public MyTank(int x, int y) {
        super(x, y);
    }

    public void setWalls(Vector<Wall> walls) {
        this.walls = walls;
    }

    public void setLiveNum(int liveNum) {
        this.liveNum = liveNum;
    }

    public void shotEnemyTank(int shootSpeed, int shootNum) {
        //如果同时存在的子弹数量超过指定数量，则不允许再发射
        if (shots.size() < shootNum) {
            switch (getDirect()) {
                case 0://上
                    shot = new Shot(getX() + 20, getY(), 0);
                    shot.setSpeed(shootSpeed);
                    break;
                case 1://右
                    shot = new Shot(getX() + 60, getY() + 20, 1);
                    shot.setSpeed(shootSpeed);
                    break;
                case 2://下
                    shot = new Shot(getX() + 20, getY() + 60, 2);
                    shot.setSpeed(shootSpeed);
                    break;
                case 3://左
                    shot = new Shot(getX(), getY() + 20, 3);
                    shot.setSpeed(shootSpeed);
                    break;
            }

            shots.add(shot);
            new Thread(shot).start();

        }

    }

    //判断坦克是否撞墙
    public boolean isOverWall() {
        switch (this.getDirect()) {
            case 0: //上
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if (((this.getY() >= wall.y) && (this.getY() <= (wall.y + 20))) &&
                            (((this.getX() > wall.x) && (this.getX() < (wall.x + 20)))
                                    || (((this.getX() + 40) > wall.x) && ((this.getX() + 40) < (wall.x + 20))))) {
                        return true;
                    }
                }
                break;
            case 1: //右
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if ((((this.getX() + 60) >= wall.x) && ((this.getX() + 60) <= (wall.x + 20))) &&
                            (((this.getY() > wall.y) && (this.getY() < (wall.y + 20)))
                                    || (((this.getY() + 40) > wall.y) && ((this.getY() + 40) < (wall.y + 20))))) {
                        return true;

                    }
                }
                break;
            case 2: //下
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if ((((this.getY() + 60) >= wall.y) && ((this.getY() + 60) <= (wall.y + 20))) &&
                            (((this.getX() > wall.x) && (this.getX() < (wall.x + 20)))
                                    || (((this.getX() + 40) > wall.x) && ((this.getX() + 40) < (wall.x + 20))))) {
                        return true;
                    }
                }
                break;
            case 3: //左
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if (((this.getX() >= wall.x) && (this.getX() <= (wall.x + 20))) &&
                            (((this.getY() > wall.y) && (this.getY() < (wall.y + 20)))
                                    || (((this.getY() + 40) > wall.y) && ((this.getY() + 40) < (wall.y + 20))))) {
                        return true;

                    }
                }
                break;
        }
        return false;
    }

    @Override
    public void run() {

    }
}
